473,461 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# Import xls file into sql server

Hi can anybody tell what is error in following code, its running through .Net 2005 editor fine,
but when running on browser gives error:
No error message available, result code: E_FAIL(0x80004005).
Expand|Select|Wrap|Line Numbers
  1.  try
  2.         {
  3.             // Create Connection to Excel Workbook
  4.  
  5.             OleDbConnection connection = new OleDbConnection(excelConnectionString);
  6.  
  7.             if (connection.State == ConnectionState.Closed)
  8.             {
  9.  
  10.                 connection.Open();
  11.                      }
  12.  
  13.             OleDbCommand command = new OleDbCommand("select Name,Age,City,Phone FROM [Data$]", connection);
  14.  
  15.             // Create DbDataReader to Data Worksheet
  16.  
  17.             OleDbDataReader dr = command.ExecuteReader();
  18.  
  19.                      // SQL Server Connection String
  20.             string sqlConnectionString = "Data Source=localhost;database=demo;user id=sa;pwd=sa";
  21.  
  22.             // Bulk Copy to SQL Server
  23.  
  24.             SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlConnectionString);
  25.  
  26.                      bulkcopy.DestinationTableName = "ExcelData";
  27.  
  28.             //            bulkCopy.WriteToServer(dr);
  29.             bulkcopy.WriteToServer(dr);
  30.             bulkcopy.Close();
  31.             if (!dr.IsClosed)
  32.                 dr.Close();
  33.  
  34.    Response.Write("Data Imported From Xls file");
  35.             if (connection.State == ConnectionState.Open)
  36.                 connection.Close();
  37.         }
  38.  
  39.         catch (Exception ex)
  40.         {
  41.             Response.Write(ex.Message);
  42.         }
Dec 14 '07 #1
4 10899
Frinavale
9,735 Expert Mod 8TB
I think it might have something to do with your connection string.

See this article. It could really help you out.
Dec 14 '07 #2
I think it might have something to do with your connection string.

See this article. It could really help you out.
Thanks Frinavale,

It works,
I had not put the file in my application folder,
I had just put the file in my application folder and its worked

Thanks for help
Dec 15 '07 #3
I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.


Thanks Frinavale,

It works,
I had not put the file in my application folder,
I had just put the file in my application folder and its worked

Thanks for help
Oct 22 '08 #4
Frinavale
9,735 Expert Mod 8TB
I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.
This forum is dedicated to helping you understand your problems so that you can create your own solution. Please attempt to solve the problem before asking for help with it so that we can address any difficulties you face.

What have you tried so far?
What problems have you encountered?

Please post any code that you think might be causing the problem (remember to use code tags when posting code snippets)

-Frinny
Oct 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Benny | last post by:
Dear All, I am performing a data import on the SQL server. Due to fact that I use the excel file as a source. Some of cells in excel are actually empty, they become NULL fields after importing...
4
by: Kevin Forbes | last post by:
Hi there, When importing a text file using SQL Server, how do I specify the path to a file on the same remote server that SQL Server is running on? I tried //c:/filename but that doesn't seem to...
2
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql...
1
by: Chris | last post by:
Background: I am using a MS Access 2000 front end with SQL Server 8.0 back end. I have the requirement to import all text files (regardless of filename) from a given folder on the network into a...
1
by: Ralph Noble | last post by:
I have two avenues to access my primary SQL Server. (I work remotely using a VPN connection.) Usually, I hit the server from my local machine but I also login to a desktop machine inside the...
2
by: s.hong | last post by:
Hello? On the ASP.NET, I can use the class by import declation on the top of page. <%@ page language="vb" explicit="true" aspcompat="false" debug="true"%> <%@ import namespace="System.Data" %>...
1
by: David Berry | last post by:
Hi All. I'm looking for any help or sample code that can show me how to make a file import wizard in ASP.NET (VB preferred) like the one that MS Access uses. I'm working on a web site where the...
4
by: BostonNole | last post by:
I am looking for suggestions on the most efficient way to import 7 different fixed width files into a DataSet. Not all at the same time. One file at a time, but the format could change from file...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
0
by: A3AN | last post by:
Hi. I receive a database backup on a daily basis. I then import this dump on another server which I use for software development. There is two db's being hosted on this server. We test software...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.